package cn.ssq.controller;

import cn.ssq.dao.BacterialStrainInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/aaaa")
public class Test2Conrolller {

    @Autowired
    private BacterialStrainInfoMapper bacterialStrainInfoMapper;

    @RequestMapping(value= "/addCrup",method = RequestMethod.POST)
    public String addCrup(@RequestParam("word_addr") MultipartFile[] word_addr, @RequestParam("txt_Addr") MultipartFile[] txt_Addr, @RequestParam("picture_Addr") MultipartFile[] picture_Addr){

        String wordsuffer = "";
        String iwordsuffer = "";
        String txtsuffer = "";
        String itxtsuffer = "";
        String picsuffer = "";
        String ipicsuffer = "";

        //分别用来存放三个集合上传完成之后的路径
        List<String> wordStrings = new ArrayList<String>();
        List<String> txtStrings = new ArrayList<String>();
        List<String> picStrings = new ArrayList<String>();

        //循环获取三个上传集合的后缀名称
        for (int i=0;i<word_addr.length;i++){
            //获取上传文件的名称
             wordsuffer =  word_addr[i].getOriginalFilename().substring(word_addr[i].getOriginalFilename().lastIndexOf("."));
             txtsuffer =  txt_Addr[i].getOriginalFilename().substring(txt_Addr[i].getOriginalFilename().lastIndexOf("."));
             picsuffer =  picture_Addr[i].getOriginalFilename().substring(picture_Addr[i].getOriginalFilename().lastIndexOf("."));

        //创建一个上传的文件路径
        String path = "D:/myfile/";

        long l = System.currentTimeMillis();
        String s = String.valueOf(l);

        //拼接上传的全路径
        String finalWordPath = path+iwordsuffer+s+wordsuffer;
        String finalTxtPath = path+itxtsuffer+s+txtsuffer;
        String finalPicPath = path+ipicsuffer+s+picsuffer;

        wordStrings.add(finalWordPath);
        txtStrings.add(finalTxtPath);
        picStrings.add(finalPicPath);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {

            }
        }
        for (int i=0;i<word_addr.length;i++){
            try {
                //进行上传操作
                word_addr[i].transferTo(new File(wordStrings.get(i)));
                txt_Addr[i].transferTo(new File(txtStrings.get(i)));
                picture_Addr[i].transferTo(new File(picStrings.get(i)));
                //循环把上传完成之后的路径存到list集合里面
            } catch (IOException e) {
                e.printStackTrace();
                System.err.println(e);
            }
        }
        /*//下面正常写
        for (int j=0;j<wordStrings.size();j++){
            BacterialStrainInfo bacterialStrainInfo = new BacterialStrainInfo();
            bacterialStrainInfo.setTxtAddr(txtStrings.get(j));
            bacterialStrainInfo.setWordAddr(wordStrings.get(j));
            bacterialStrainInfo.setPictureAddr(picStrings.get(j));
            bacterialStrainInfoMapper.insertInto(bacterialStrainInfo);
        }*/
            return "sueecss";
    }

    @RequestMapping("/aaa")
    public void aaa(){
        System.out.println("1233");
    }

}
